package com.example.aopdemo.aop;

import android.view.View;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @author DaQiang
 * @date 2020/4/15 13:55
 * @desc 防快速点击
 */
@Aspect
public class AspectTest {
    final String TAG = AspectTest.class.getSimpleName();
    private boolean canDoubleClick = false;
    private View mLastView;


    @Before("@annotation(com.example.aopdemo.aop.DoubleClick)")
    public void beforeEnableDoubleClcik(JoinPoint joinPoint) throws Throwable {
        canDoubleClick = true;
    }

    @Around("execution(* android.view.View.OnClickListener.onClick(..))")
    public void onClickLitener(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] objects = joinPoint.getArgs();
        View view = objects.length == 0 ? null : (View) objects[0];
        boolean b = !NoDoubleClickUtils.isDoubleClick();
        boolean re = b || (view != mLastView) || canDoubleClick;
        if (re) {
            //执行原来的方法
            joinPoint.proceed();
            canDoubleClick = false;
        }
        mLastView = view;
    }

}